<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/15
 * Time: 23:28
 */
namespace Admin\Controller;

class ProjectsController extends BaseController
{
    private $project = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->project = D('Project');
    }

    public function projectsList()
    {
       //查询满足要求的总记录数
       $count = $this->project->count();
       //实例化分页类 传入总记录数和每页显示的记录数(10)
       $Page = new \Think\Page($count,10);
       $Page->setConfig('next',C('PAGE_CONFIG.next'));
       $Page->setConfig('prev',C('PAGE_CONFIG.prev'));
       $Page->setConfig('last',C('PAGE_CONFIG.last'));
       $Page->setConfig('first',C('PAGE_CONFIG.first'));
       $Page->lastSuffix=C('PAGE_CONFIG.lastSuffix');//在此设置一行
       $show = $Page->show();// 分页显示输出
       //进行分页数据查询 注意limit方法的参数要使用Page类的属性
       $projects = $this->project->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();

       $this->assign(array(
           // 赋值数据集
           'projects' => $projects,
           // 赋值分页输出
           'show' => $show
       ));
       $this->display('Projects/list');
   }

   public function projectsAdd()
   {
      if (!IS_POST) {
          $this->display('Projects/add');
          exit;
      }
      if ($this->project->projectsAdd()) {
          $this->ajaxReturn('true');
      } else {
          $this->ajaxReturn($this->project->getError());
      }
   }

   public function projectsModify()
   {
       $project_id = I('get.id');
       if (!IS_POST) {
           $project = $this->project->where(array('id' => array('eq', $project_id)))->find();
           $project['content'] = htmlspecialchars_decode($project['content']);
           $this->assign(array('project' => $project));
           $this->display('Projects/edit');
           exit;
       }
       if($this->project->projectsModify()) {
           $this->ajaxReturn('true');
       } else {
           $this->ajaxReturn($this->project->getError());
       }
   }

   public function show()
   {

   }

   public function projectsDel()
   {
       $id = I('get.id');
       $project = $this->project->find($id);

       unlink(APP_PATH.$project['image']);
       unlink(APP_PATH.$project['mobile_image']);

       $result = $this->project->delete($id);
       if ($result !== false) {
           $this->ajaxReturn('true');
       } else {
           $this->ajaxReturn($this->project->getError());
       }
   }
}